package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javax.xml.validation.Validator;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.EmployeeDao;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.Department;
import com.pentofab.payrollmanagement.exception.DatabaseException;
import com.pentofab.payrollmanagement.ui.control.CustomTextField;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.TextField;

public class AddDepartmentUIController extends AbstractUIController implements
		FormValidator {

	@FXML
	Parent root;
	@FXML
	CustomTextField codeField;
	@FXML
	TextField descriptionField;

	private Company company;
	private Department department;
	private boolean updateMode = false;

	@Override
	public Parent getView() {
		return root;
	}

	public void save() {
		if (isValid()) {
			EmployeeDao empDao = PayrollApplicationFactory.getEmployeeMgmt();
			Department dept = new Department();
			dept.setCompanyCode(company.getCompanyID());
			dept.setCode(codeField.getText());
			dept.setDescription(descriptionField.getText());
			try {

				if (!updateMode) {
					empDao.addDepartment(dept);
					showInfo(String.format(UIMessages.DEPARTMENT_ADD_SUCCESS,
							dept.getCode()));
				} else {
					empDao.updateDepartment(dept);
					showInfo(String.format(
							UIMessages.DEPARTMENT_UPDATE_SUCCESS,
							dept.getCode()));
				}
			} catch (DatabaseException e) {
				String message = "";
				switch (e.getCode()) {
				case DatabaseException.DUPLICATE_ENTRY:
					message = UIMessages.DUPLICATE_DEPARTMENT_CODE;
					break;
				case DatabaseException.UNKNOWN_ERROR:
				default:
					message = UIMessages.UNKNOW_DATABASE_ERROR;
					break;
				}
				showError(message);
			} catch (Exception e) {
				showError(UIMessages.UNKNOW_DATABASE_ERROR);
			}
		} else {
			showError(getValidationMessage());
		}
	}

	public void reset() {
		if (this.department != null) {
			refreshUI(this.company, this.department);
		} else {
			refreshUI(this.company);
		}
	}

	public void goBack() {
		if (getParent() != null) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}
	}

	@Override
	public boolean isValid() {
		if (codeField.getText().length() != 2) {
			return false;
		}
		if (descriptionField.getText().length() == 0) {
			return false;
		}
		return true;
	}

	@Override
	public String getValidationMessage() {
		if (codeField.getText().length() != 2) {
			return "Department code should be exactly 2 characters";
		}
		if (descriptionField.getText().length() == 0) {
			return "Description cannot be empty";
		}
		return null;
	}

	@Override
	public String getTitle() {
		return UIMessages.UITitles.DEPARTMENT_ADD;
	}

	@Override
	public void refreshUI(Object... args) {
		if (args != null && args.length > 0) {
			this.company = (Company) args[0];
			this.codeField.clear();
			this.descriptionField.clear();
		}
		if (args != null && args.length > 1) {
			this.updateMode = true;
			this.department = (Department) args[1];
			this.codeField.setText(department.getCode());
			this.codeField.disableProperty().set(true);
			this.descriptionField.setText(department.getDescription());
		} else {
			this.updateMode = false;
			this.codeField.clear();
			this.descriptionField.clear();
		}
	}
}
